package com.yingxin.yx.framework.commons.utlis;

import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yx.framework.commons.helper.DecimalHelper;

import java.math.BigDecimal;
import java.util.regex.Pattern;

public class SpecHelper {

    public static void main(String[] args) {

        String a = "1";
        if( !a.trim().equals( "1" ) && !a.trim().equals( "2" ) ){
            System.out.println("aabbccddeeffgg");
        }

        System.out.println( SpecHelper.formatSpec( "3.25*1220" ) );
    }

    public static String specPattern = "(([0-9])|([1-9]([0-9]+)))(.[0-9]+)?(\\*)(([0-9])|([1-9]([0-9]+)))(.[0-9]+)?";


    public static String buildSpec(BigDecimal thickness, BigDecimal width) {
        return DecimalHelper.stripTrailingZeros(thickness) + "*" + DecimalHelper.stripTrailingZeros(width);
    }

    public static String buildSpec(String thickness, BigDecimal width) {
        BigDecimal t = new BigDecimal(thickness);
        return DecimalHelper.stripTrailingZeros(t) + "*" + DecimalHelper.stripTrailingZeros(width);
    }

    public static String buildSpec(BigDecimal thickness, String width) {
        BigDecimal w = new BigDecimal(width);
        return DecimalHelper.stripTrailingZeros(thickness) + "*" + DecimalHelper.stripTrailingZeros(w);
    }

    public static String buildSpec(String thickness, String width) {
        BigDecimal t = new BigDecimal(thickness);
        BigDecimal w = new BigDecimal(width);
        return DecimalHelper.stripTrailingZeros(t) + "*" + DecimalHelper.stripTrailingZeros(w);
    }


    /**
     * 格式化规格格式
     * @param spec
     * @return
     */
    public static String formatSpec(String spec) {
        boolean isMatch = Pattern.matches(specPattern, spec.trim());

        String errorMsg = "";

        if (isMatch) {
            String[] specs = spec.split("\\*");
            // MaterialSpecDto specDto = new MaterialSpecDto();
            // specDto.setSpec(spec);
            try {
                // specDto.setThickness(new BigDecimal(specs[0]));
                // specDto.setWidth(new BigDecimal(specs[1]));
                // 厚度校验
                BigDecimal t = new BigDecimal(specs[0]);
                if (t.compareTo(new BigDecimal(0.05)) < 0
                        || t.compareTo(new BigDecimal(10)) > 0) {
                    errorMsg = "厚度请输入[0.05-10]之间的值";
                    throw new BizException(errorMsg);
                }

                // 宽度校验
                BigDecimal w = new BigDecimal(specs[1]);
                if (w.compareTo(new BigDecimal(600)) < 0
                        || w.compareTo(new BigDecimal(1500)) > 0) {
                    errorMsg = "宽度请输入[600-1500]之间的值";
                    throw new BizException(errorMsg);
                }
                String p1 = DecimalHelper.stripTrailingZeros(t);
                if (!p1.contains(".")) {
                    p1 = p1 + ".0";
                }
                return p1 + "*" + DecimalHelper.stripTrailingZeros(w);
            } catch (Exception e) {
                throw new BizException(errorMsg);
            }
        } else {
            throw new BizException("规格异常");
        }
    }

    public static BigDecimal getThickness(String spec) {
        boolean isMatch = Pattern.matches(specPattern, spec.trim());
        if (isMatch) {
            String[] specs = spec.split("\\*");
            try {
                return new BigDecimal(specs[0]);
            } catch (Exception e) {
                throw new BizException("规格异常");
            }
        } else {
            throw new BizException("规格异常");
        }
    }

    public static BigDecimal getWidth(String spec) {
        boolean isMatch = Pattern.matches(specPattern, spec.trim());
        if (isMatch) {
            String[] specs = spec.split("\\*");
            try {
                return new BigDecimal(specs[1]);
            } catch (Exception e) {
                throw new BizException("规格异常");
            }
        } else {
            throw new BizException("规格异常");
        }
    }


    public static String convertSpec(String saleSpec) {
        BigDecimal thickness = SpecHelper.getThickness(saleSpec);
        BigDecimal width = SpecHelper.getWidth(saleSpec);
        BigDecimal convertThickness;
        if (thickness.compareTo(BigDecimal.valueOf(0.18)) <= 0) {
            convertThickness = BigDecimal.valueOf(0.4);
        } else if (thickness.compareTo(BigDecimal.valueOf(0.19)) >= 0 && thickness.compareTo(BigDecimal.valueOf(0.23)) <= 0) {
            convertThickness = BigDecimal.valueOf(0.5);
        } else if (thickness.compareTo(BigDecimal.valueOf(0.24)) >= 0) {
            convertThickness = BigDecimal.valueOf(0.6);
        } else {
            throw new BizException("厚度异常");
        }
        return buildSpec(convertThickness, width);
    }

    /*
    public static void main(String[] args) {

    }
    */

}
